package com.copa.module.order.manage.impl;

import cn.hutool.core.util.ObjectUtil;
import com.copa.domain.ResponseDto;
import com.copa.module.goods.domain.dto.GoodsProductDto;
import com.copa.module.goods.manage.GoodsManage;
import com.copa.module.order.domain.dto.OrderDetailDto;
import com.copa.module.order.domain.dto.OrderLogDto;
import com.copa.module.order.domain.entity.OrderEntity;
import com.copa.module.order.domain.entity.OrderGoodsEntity;
import com.copa.module.order.domain.entity.OrderLogEntity;
import com.copa.module.order.manage.OrderManage;
import com.copa.module.order.mapper.OrderGoodsDao;
import com.copa.module.order.mapper.OrderLogDao;
import com.copa.module.user.domain.dto.UserAddressDto;
import com.copa.module.user.manage.UserAddressManage;
import com.google.common.collect.Maps;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author copa
 * @createDate 2021-01-09 16:26
 * @function
 */
@Service
public class OrderManageImpl implements OrderManage {

    @Autowired
    private OrderGoodsDao orderGoodsDao;

    @Autowired
    private UserAddressManage userAddressManage;

    @Autowired
    private OrderLogDao orderLogDao;

    @Override
    public OrderDetailDto findOrderDetailByOrderId(OrderEntity order) {
        OrderDetailDto orderDetailDto = new OrderDetailDto();
        // 查找子订单
        List<GoodsProductDto> goodsProductDtos = orderGoodsDao.findGoodsAndOrderGoodsByOrderId(order.getId());
        // 查找用户以及地址信息
        ResponseDto<UserAddressDto> userAddressRes = userAddressManage.findUserAddressByUserIdAndAddressId(order.getUserId(), order.getAddressId());
        // 查找订单日志
        List<OrderLogDto> orderLogList = orderLogDao.findOrderLogByOrderId(order.getOrderSn());
        orderDetailDto.setOrderGoods(goodsProductDtos);
        orderDetailDto.setUserAddressDto(userAddressRes.getData());
        orderDetailDto.setOrderLogList(orderLogList);
        return orderDetailDto;
    }

    @Override
    public ResponseDto<OrderGoodsEntity> findOrderGoodsByOrderGoodsId(String subOrderId) {
        OrderGoodsEntity orderGoods = orderGoodsDao.selectById(subOrderId);
        if (ObjectUtil.isNotNull(orderGoods)) {
            return ResponseDto.success(orderGoods);
        }
        return ResponseDto.errorMsg("查不到子订单！");
    }

    @Override
    public ResponseDto<Boolean> putOrderGoodsById(OrderGoodsEntity orderGoods) {
        int update = orderGoodsDao.updateById(orderGoods);
        if (update > 0) {
            return ResponseDto.successMsg("修改成功");
        }
        return ResponseDto.errorMsg("系统繁忙，请稍后再试！");
    }

    @Override
    public ResponseDto<List<OrderGoodsEntity>> findOrderGoodsListByOrderId(String orderId) {
        Map<String, Object> conditionMaps = Maps.newHashMap();
        conditionMaps.put("order_id", orderId);
        List<OrderGoodsEntity> orderGoodsEntities = orderGoodsDao.selectByMap(conditionMaps);
        if (ObjectUtil.isNotNull(orderGoodsEntities) && ObjectUtil.length(orderGoodsEntities) > 0) {
            return ResponseDto.success(orderGoodsEntities);
        }

        return ResponseDto.errorMsg("查不到子订单！");
    }
}
